package com.open.api.peekapi.controller;

import com.jfinal.plugin.activerecord.Db;
import com.open.api.peekapi.bean.ApiItem;
import com.open.api.peekapi.services.ApiItemDataServices;
import com.open.api.peekapi.services.ApiItemServices;
import com.open.api.peekapi.util.HelpMe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mengdexuan on 2017/6/21 13:10.
 */
@RequestMapping("/back")
@Controller
public class BackController {


	private static Logger log = LoggerFactory.getLogger(BackController.class);

	@Autowired
	ApiItemServices apiItemServices;
	@Autowired
	ApiItemDataServices apiItemDataServices;


	@RequestMapping("/")
	public String index(HttpServletRequest request, Model model,Integer switchModel,String parentId){

		if (switchModel==null)switchModel=1;

		model.addAttribute("switchModel",switchModel);
		List<ApiItem> firstLevelList = apiItemServices.firstLevelItemAll();
		model.addAttribute("firstLevelList",firstLevelList);
		if (switchModel==2){
			List<ApiItem> secondLevelList = null;
			if (HelpMe.isNull(parentId)){
				ApiItem one = firstLevelList.get(0);
				parentId = one.getId();
			}
			secondLevelList = apiItemServices.secondLevelByFirstLevelId(parentId);
			String parentName = Db.queryStr("select name from api_item where id = ?", new Object[]{parentId});
			model.addAttribute("parentId",parentId);
			model.addAttribute("parentName",parentName);
			model.addAttribute("secondLevelList",secondLevelList);
		}


		return "back_index";
	}




	@RequestMapping("/changeStatus")
	@ResponseBody
	public String changeStatus(String id){

		boolean flag = apiItemServices.changeStatus(id);

		return "true";
	}


	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){

		boolean flag = apiItemServices.del(id);
		if (!flag){
			return "项目中存在关联的模块！";
		}
		return "true";
	}


	@RequestMapping("/del2")
	@ResponseBody
	public String del2(String id){

		boolean flag = apiItemServices.del2(id);
		if (!flag){
			return "目录存在关联的数据！";
		}
		return "true";
	}



	@RequestMapping("/add")
	@ResponseBody
	public String add(ApiItem apiItem){

		ApiItem one = apiItemServices.add(apiItem);

		return "true";
	}



}







































